package com.juyueange.user.wxapi;
/*
 * -----------------------------------------------------------------
 * Copyright (C) 2018-2020, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * File: PayActivity.java
 * Author: Carl
 * Version: V100R001C01
 * Create: 2018/5/8 17:23
 * Changes (from 2018/5/8)
 * -----------------------------------------------------------------
 * 2018/5/8 : Create PayActivity.java (Carl);
 * -----------------------------------------------------------------
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.juyueange.user.Constants;
import com.juyueange.user.R;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class  WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private String TAG = "PayDemo";
    private IWXAPI api;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entry);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }
    @Override
    public void onReq(BaseReq baseReq) {
        Toast.makeText(this, "openid = " + baseReq.openId, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResp(BaseResp resp) {
        String result="";
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    result = "支付成功";
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    result = "支付取消";
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:
                    result = "支付被拒绝";
                    break;
                case BaseResp.ErrCode.ERR_UNSUPPORT:
                    result = "不支持错误";
                    break;
                default:
                    result = "支付返回,支付失败" ;
                    break;
            }
        }
        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        this.finish();
    }
}